﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace DBSourceToolsLib.CodeParser
{
    public class CodeParser
    {
        private List<ParseObject> m_lstParseObject = new List<ParseObject>();
        private BraceNode m_Chain;

        public List<ParseObject> LstParseObject
        {
            get { return m_lstParseObject; }
            set { m_lstParseObject = value; }
        }

        public bool StartsWithSelect(string strExecuteText)
        {
            string [] strArr = strExecuteText.Split(new char[] { '\t', ' ', '\n', '\r' });
            foreach (string strString in strArr)
            {
                if (!string.IsNullOrEmpty(strString))
                {
                    if (strString.StartsWith("select", StringComparison.OrdinalIgnoreCase))
                        return true;
                    else
                        return false;
                }
            }
            return false;
        }

        public void BuildStack(string strLine, int iLineIdx)
        {
            m_lstParseObject = CodeParser.Parse(strLine, iLineIdx);
        }

        public string BuildFullString()
        {
            StringBuilder oStringBuilder = new StringBuilder();

            for (int i = 0; i < LstParseObject.Count; i++)
            {
                oStringBuilder.AppendLine(i.ToString() + " " + LstParseObject[i].DumpToString());
            }
            return oStringBuilder.ToString();
        }

        public CodeParser()
        {
            ParseTable.ClearTableList();
            m_Chain = new BraceLiteralNode();
            BraceBraceNode oBraceNode = new BraceBraceNode();
            m_Chain.addToChain(oBraceNode);
        }

        public bool ResolveBraces(ref SourceFile oSourceFile)
        {
            oSourceFile.ClearErrors();
            m_Chain.sendtoChain(ref oSourceFile);
            return true;
        }

        public static List<ParseObject> Parse(string strParseString, int iLineIdx)
        {
            List<ParseObject> oParseObjectList = new List<ParseObject>();

            char[] arrChars = strParseString.ToCharArray();

            string strCurrentValue = "";

            for (int i = 0; i < arrChars.Length; i++)
            {
                char currChar = arrChars[i];
                object oNextChar = null;

                // peek the next char
                if ((i + 1) < arrChars.Length)
                    oNextChar = arrChars[i + 1];

                if (ParseFactory.IsCharacterRecognized(currChar))
                {
                    if (strCurrentValue.Length > 0)
                    {
                        //m_stackParseObject.Push(ParseFactory.GetParseObject(strCurrentValue));
                        oParseObjectList.Add(ParseFactory.GetParseObject(strCurrentValue, iLineIdx, oParseObjectList.Count));
                    }

                    //m_stackParseObject.Push(ParseFactory.GetParseObjectFromChar(currChar));
                    oParseObjectList.Add(ParseFactory.GetParseObjectFromChar(currChar, iLineIdx, oParseObjectList.Count));
                    strCurrentValue = "";

                }
                else
                {
                    strCurrentValue += currChar;
                    if (ParseFactory.IsStringRecognized(strCurrentValue, oNextChar))
                    {
                        oParseObjectList.Add(ParseFactory.GetParseObject(strCurrentValue, iLineIdx, oParseObjectList.Count));
                        strCurrentValue = "";
                    }
                }
            }
            if (strCurrentValue.Length > 0)
            {
                //m_stackParseObject.Push(ParseFactory.GetParseObject(strCurrentValue));
                oParseObjectList.Add(ParseFactory.GetParseObject(strCurrentValue, iLineIdx, oParseObjectList.Count));
            }


            return oParseObjectList;
        }
    }
}
